<html>
<head><meta charset="utf-8"><title>Memory layout of DST · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html">Memory layout of DST</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230362689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230362689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230362689">(Mar 15 2021 at 15:20)</a>:</h4>
<p>I'm trying to figure out how to emit DST for a rust verification backend I'm working on.  In particular, what is the memory layout of the a <code>[u8]</code> without the reference (i.e. the <code>Inner.inner</code> field? What is the layout of the <code>Inner</code> class here, what is the layout of <code>&amp;Inner</code> (I assume a fat pointer)? </p>
<div class="codehilite"><pre><span></span><code>// From rust/library/std/src/sys_common/os_str_bytes.rs
//      rust/library/std/src/ffi/os_str.rs

use std::mem;

struct Inner {
    pub inner: [u8],
}

impl Inner {
    fn from_u8_slice(s: &amp;[u8]) -&gt; &amp;Inner {
        unsafe { mem::transmute(s) }
    }
}

 fn test2() {
     let inner = Inner::from_u8_slice(b&quot;hi&quot;);
     assert!(inner.inner[0] == &#39;h&#39; as u8);
     assert!(inner.inner[1] == &#39;i&#39; as u8);
 }

fn main() {
    test1();
    test2();
}

fn test1() {
    let inner = Inner::from_u8_slice(b&quot;hi&quot;);
    let b = &amp;inner.inner;
    assert!(b[0] == &#39;h&#39; as u8);
    assert!(b[1] == &#39;h&#39; as u8);
}
</code></pre></div>



<a name="230362961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230362961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230362961">(Mar 15 2021 at 15:21)</a>:</h4>
<p>[u8] would be just a list of bytes where the value is placed.</p>



<a name="230363091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363091">(Mar 15 2021 at 15:22)</a>:</h4>
<p>&amp;Inner is a fat pointer, yes.</p>



<a name="230363241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363241">(Mar 15 2021 at 15:23)</a>:</h4>
<p>So in C terms, </p>
<div class="codehilite"><pre><span></span><code>struct Inner {
  uint8_t* inner;
};
</code></pre></div>



<a name="230363333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363333">(Mar 15 2021 at 15:23)</a>:</h4>
<p>And to abuse C syntax:</p>
<div class="codehilite"><pre><span></span><code>struct &amp;Inner {
    struct Inner inner,
   size_t size
};
</code></pre></div>



<a name="230363346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363346">(Mar 15 2021 at 15:23)</a>:</h4>
<p>Is that correct?</p>



<a name="230363627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363627">(Mar 15 2021 at 15:25)</a>:</h4>
<p>not really, its a </p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">struct</span> <span class="nc">Inner</span> <span class="p">{</span>
    <span class="kt">uint8_t</span> <span class="n">inner</span><span class="p">[</span><span class="n">UNKNOWN</span><span class="p">];</span> <span class="c1">// if I remember my C right</span>
<span class="p">}</span>
</code></pre></div>
<p>and <code>&amp;Inner</code> is a </p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">struct</span> <span class="o">&amp;</span><span class="n">Inner</span> <span class="p">{</span>
    <span class="k">struct</span> <span class="nc">Inner</span><span class="o">*</span> <span class="n">buffer</span><span class="p">;</span>
    <span class="kt">size_t</span> <span class="n">size</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>



<a name="230363766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363766">(Mar 15 2021 at 15:26)</a>:</h4>
<p>Ahh ...</p>



<a name="230363980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230363980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230363980">(Mar 15 2021 at 15:28)</a>:</h4>
<p>And if I had more than one field? </p>
<div class="codehilite"><pre><span></span><code>struct Inner {
    pub foo: i16,
    pub inner: [u8],
}
</code></pre></div>



<a name="230364220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364220">(Mar 15 2021 at 15:29)</a>:</h4>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">struct</span> <span class="nc">Inner</span> <span class="p">{</span>
    <span class="kt">int64_t</span> <span class="n">foo</span><span class="p">;</span>
    <span class="kt">uint8_t</span> <span class="n">inner</span><span class="p">[</span><span class="n">UNKNOWN</span><span class="p">];</span>
<span class="p">}</span>
</code></pre></div>



<a name="230364347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364347">(Mar 15 2021 at 15:30)</a>:</h4>
<p>And the size field in the fat pointer would be the size of the known fields of <code>Inner</code> + the size of the slice at the end?</p>



<a name="230364407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364407">(Mar 15 2021 at 15:30)</a>:</h4>
<p>No, the size field in the fat pointer only counts the amount of elements in <code>[u8]</code>.</p>



<a name="230364521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364521">(Mar 15 2021 at 15:31)</a>:</h4>
<p>Then the allocator does the addition when doing an allocation?</p>



<a name="230364733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364733">(Mar 15 2021 at 15:32)</a>:</h4>
<p>No, <code>std::mem::size_of_val</code> does this addition.</p>



<a name="230364791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364791">(Mar 15 2021 at 15:32)</a>:</h4>
<p>ahh, ok</p>



<a name="230364877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230364877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Schwartz-Narbonne <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230364877">(Mar 15 2021 at 15:33)</a>:</h4>
<p>I'm going to go implement this, I may have further questions this afternoon. Thanks for the help!</p>



<a name="230417251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230417251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230417251">(Mar 15 2021 at 20:58)</a>:</h4>
<p>actually C can have arrays without explicit size inside structs</p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">struct</span> <span class="nc">Inner</span> <span class="p">{</span>
  <span class="kt">uint8_t</span> <span class="n">inner</span><span class="p">[];</span>
<span class="p">}</span>
</code></pre></div>
<p>and it is actually safer to do that, because these arrays must reside at the end of struct</p>



<a name="230420626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230420626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230420626">(Mar 15 2021 at 21:21)</a>:</h4>
<p>yeah, I wasn't sure if C represents that as a pointer or as a value, so went for the safe option to demonstrate my point.</p>



<a name="230420659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230420659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230420659">(Mar 15 2021 at 21:21)</a>:</h4>
<p>Its been ages since I last wrote any serious C ^^</p>



<a name="230534820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230534820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230534820">(Mar 16 2021 at 15:38)</a>:</h4>
<p>Yeah, C likes to cast arrays to pointers</p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="kt">void</span> <span class="nf">takes_pointer_in_reality</span><span class="p">(</span><span class="kt">int</span> <span class="n">arr</span><span class="p">[])</span> <span class="p">{}</span>
<span class="kt">void</span> <span class="nf">takes_pointer_in_reality2</span><span class="p">(</span><span class="kt">int</span> <span class="n">arr</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span> <span class="p">{}</span>
</code></pre></div>



<a name="230535150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20DST/near/230535150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20DST.html#230535150">(Mar 16 2021 at 15:40)</a>:</h4>
<p>but</p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">struct</span> <span class="nc">Array</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">arr</span><span class="p">[</span><span class="mi">3</span><span class="p">];</span>
<span class="p">};</span>

<span class="kt">void</span> <span class="nf">takes_array</span><span class="p">(</span><span class="k">struct</span> <span class="nc">Array</span> <span class="n">arr</span><span class="p">)</span> <span class="p">{}</span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>